
import 'package:flutter/widgets.dart';

class BoxDecorationTween extends Tween<BoxDecoration> {
  BoxDecorationTween({ BoxDecoration begin, BoxDecoration end }) : super(begin: begin, end: end);
   @override
  BoxDecoration lerp(double t) => BoxDecoration.lerp(begin, end, t);
}

class AnimatedBoxDecoration extends ImplicitlyAnimatedWidget {
  final BoxDecoration decoration;
  final Widget child;
  const AnimatedBoxDecoration({
    Key key,
    @required this.decoration,
    this.child,
    Curve curve = Curves.linear,
    @required Duration duration,
    Duration reverseDuration,
  }) : assert(decoration != null),
       super(key: key, curve: curve, duration: duration);

  @override
  _AnimatedBoxDecorationState createState() {
    return _AnimatedBoxDecorationState();
  }
}

class _AnimatedBoxDecorationState extends ImplicitlyAnimatedWidgetState<AnimatedBoxDecoration> {
  BoxDecorationTween decoration;
  Animation<Decoration> decorationAnimation;

  @override
  void forEachTween(TweenVisitor<dynamic> visitor) {
    decoration = visitor(decoration, widget.decoration, (dynamic value) => BoxDecorationTween(begin: value));
  }

  @override
  void didUpdateTweens() {
    decorationAnimation = animation.drive(decoration);
  }

  @override
  Widget build(BuildContext context) {
    return DecoratedBoxTransition(
      decoration: decorationAnimation,
      child: widget.child,
    );
  }
}